home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / keymap / appkeymap.c next >
C/C++ Source or Header  |  1992-09-03  |  8KB  |  236 lines

  1. /*
  2. Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  
  4. This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5. use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6. published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  
  8. The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9. information on the correct usage of the techniques and operating system 
  10. functions presented in these examples.  The source and executable code 
  11. of these examples may only be distributed in free electronic form, via 
  12. bulletin board or as part of a fully non-commercial and freely 
  13. redistributable diskette.  Both the source and executable code (including 
  14. comments) must be included, without modification, in any copy.  This 
  15. example may not be published in printed form or distributed with any
  16. commercial product.  However, the programming techniques and support
  17. routines set forth in these examples may be used in the development
  18. of original executable software products for Commodore Amiga computers.
  19.  
  20. All other rights reserved.
  21.  
  22. This example is provided "as-is" and is subject to change; no
  23. warranties are made.  All use is at your own risk. No liability or
  24. responsibility is assumed.
  25.  
  26. */
  27.  
  28. /* 
  29. appkeymap.c - subroutines to copy the default keymap, modify the copy
  30.  
  31. Usage: struct KeyMap *appkeymap = CreateAppKeyMap();
  32.        void DeleteAppKeyMap(struct KeyMap *appkeymap);
  33.  
  34. This example modifies the copied keymap by unmapping all of the
  35. numeric keypad keys.  This creates a good keymap for use with either
  36. keymap.library MapANSI() or commodities InvertString().
  37. If you used a default keymap with the above functions, numeric keypad
  38. raw key values would be returned for keys which are available with
  39. fewer keypresses on numeric pad than on the normal keyboard.
  40. It is generally preferable to have the normal keyboard raw values
  41. since many applications attach special meanings to numeric pad keys.
  42. The AlterAppKeyMap() routine in this module could easilty be modified
  43. to instead set new values for numeric pad or function keys.
  44.  
  45. IMPORTANT: Do Not Use SetKeyMapDefault() unless you are a system
  46. preferences editor OR an application that takes over the machine.
  47. If you want to use a customized keymap such as this in your
  48. application, open your own Intuition window, attach a console
  49. device to it, and then use the console device SETKEYMAP command
  50. to set your console device unit to your custom keymap.
  51.  
  52. */
  53.  
  54. #include <exec/types.h>
  55. #include <exec/memory.h>
  56. #include <devices/console.h>
  57. #include <devices/keymap.h>
  58.  
  59. #include <clib/exec_protos.h>
  60. #include <clib/keymap_protos.h>
  61. #include <clib/alib_protos.h>
  62.  
  63. /* Raw keys we might want to remap which are the same on all keyboards */
  64. #define UP_KEY      0x4C
  65. #define DOWN_KEY    0x4D
  66. #define RIGHT_KEY   0x4E
  67. #define LEFT_KEY    0x4F
  68.  
  69. #define F1_KEY      0x50
  70. #define F2_KEY      0x51
  71. #define F3_KEY      0x52
  72. #define F4_KEY      0x53
  73. #define F5_KEY      0x54
  74. #define F6_KEY      0x55
  75. #define F7_KEY      0x56
  76. #define F8_KEY      0x57
  77. #define F9_KEY      0x58
  78. #define F10_KEY     0x59
  79.  
  80. #define N0_KEY      0x0F
  81. #define N1_KEY      0x1D
  82. #define N2_KEY      0x1E
  83. #define N3_KEY      0x1F
  84. #define N4_KEY      0x2D
  85. #define N5_KEY      0x2E
  86. #define N6_KEY      0x2F
  87. #define N7_KEY      0x3D
  88. #define N8_KEY      0x3E
  89. #define N9_KEY      0x3F
  90.  
  91. #define NPERIOD_KEY 0x3C
  92. #define NOPAREN_KEY 0x5A
  93. #define NCPAREN_KEY 0x5B
  94. #define NSLASH_KEY  0x5C
  95. #define NASTER_KEY  0x5D
  96. #define NMINUS_KEY  0x4A
  97. #define NPLUS_KEY   0x5E
  98. #define NENTER_KEY  0x43
  99.  
  100. #define RETURN_KEY  0x44
  101. #define HELP_KEY    0x5F
  102.  
  103.  
  104. /* Null terminated array of keys our application wants to remap or disable */
  105. UBYTE nullkeys[] =
  106. { N0_KEY,      N1_KEY,      N2_KEY,      N3_KEY,      N4_KEY,
  107.   N5_KEY,      N6_KEY,      N7_KEY,      N8_KEY,      N9_KEY,
  108.   NPERIOD_KEY, NOPAREN_KEY, NCPAREN_KEY, NSLASH_KEY,
  109.   NASTER_KEY,  NMINUS_KEY,  NPLUS_KEY,   NENTER_KEY,
  110.   0
  111. };
  112.  
  113. /* Count of elements in keymap arrays */
  114. #define MAP_SIZE  64
  115. #define TYPE_SIZE 64
  116. #define CAPS_SIZE 8
  117. #define REPS_SIZE 8
  118.  
  119. /* We allocate our Lo and Hi array pairs each as a single array */
  120. struct KeyMapArrays {
  121.    ULONG LHKeyMap[MAP_SIZE << 1];
  122.    UBYTE LHKeyMapTypes[TYPE_SIZE << 1];
  123.    UBYTE LHCapsable[CAPS_SIZE << 1];
  124.    UBYTE LHRepeatable[REPS_SIZE << 1];
  125.    };
  126.  
  127. static struct KeyMapArrays *karrays;
  128. static struct KeyMap *defkeymap, *appkeymap;
  129. static LONG mapsize;
  130.  
  131. /* externally callable functions */
  132. struct KeyMap *CreateAppKeyMap(void);
  133. void DeleteAppKeyMap(struct KeyMap *appkeymap);
  134.  
  135. /* local functions */
  136. static void CopyKeyMap(struct KeyMap *source, struct AppKeyMap *dest);
  137. static void AlterAppKeyMap(struct KeyMap *appkeymap);
  138.  
  139.  
  140. struct Library *KeymapBase = NULL;
  141.  
  142. struct KeyMap *CreateAppKeyMap()
  143.     {
  144.     if(KeymapBase = OpenLibrary("keymap.library",37L))
  145.         {
  146.         /* Get a pointer to the keymap which is set as the system default */
  147.         if(defkeymap = AskKeyMapDefault())
  148.             {
  149.             /* Allocate our KeyMap structures and arrays */
  150.             mapsize = sizeof(struct KeyMap) + sizeof(struct KeyMapArrays);
  151.             if(appkeymap = (struct KeyMap *)AllocMem(mapsize,MEMF_CLEAR))
  152.                 {
  153.                 /* Init our appkeymap fields to point to our allocated arrays.
  154.                  * Here the  + 1  means  + sizeof(struct KeyMap)  because
  155.                  * we are adding 1 to a pointer to a struct KeyMap.
  156.                  * Each LH array contains a Lo and a Hi array.
  157.                  */
  158.                 karrays = (struct KeyMapArrays *)(appkeymap + 1);
  159.                 appkeymap->km_LoKeyMap = &karrays->LHKeyMap[0];
  160.                 appkeymap->km_HiKeyMap = &karrays->LHKeyMap[MAP_SIZE];
  161.                 appkeymap->km_LoKeyMapTypes = &karrays->LHKeyMapTypes[0];
  162.                 appkeymap->km_HiKeyMapTypes = &karrays->LHKeyMapTypes[TYPE_SIZE];
  163.                 appkeymap->km_LoCapsable = &karrays->LHCapsable[0];
  164.                 appkeymap->km_HiCapsable = &karrays->LHCapsable[CAPS_SIZE];
  165.                 appkeymap->km_LoRepeatable = &karrays->LHRepeatable[0];
  166.                 appkeymap->km_HiRepeatable = &karrays->LHRepeatable[REPS_SIZE];
  167.  
  168.                 /* Copy the user's default system keymap arrays to our appkeymap
  169.                  * arrays to get the proper keymappings for the user's keyboard.
  170.                  */ 
  171.                 CopyKeyMap(defkeymap, appkeymap);
  172.  
  173.                 /* Now make our changes to our appkeymap */
  174.                 AlterAppKeyMap(appkeymap);
  175.                 }
  176.             }
  177.         CloseLibrary(KeymapBase);
  178.         }
  179.     return(appkeymap);
  180.     }
  181.  
  182.  
  183. void DeleteAppKeyMap(struct KeyMap *appkeymap)
  184.     {
  185.     if(appkeymap)  FreeMem(appkeymap, mapsize);
  186.     }
  187.  
  188.  
  189. static void AlterAppKeyMap(struct KeyMap *appkeymap)
  190. {
  191. ULONG *keymappings;
  192. UBYTE *keymaptypes;
  193. UBYTE rawkeynum;
  194. int i;
  195.  
  196. /* Our application wants numeric pad keys remapped to nothing
  197.  * so that we can use this keymap with MapANSII and NOT get back
  198.  * raw codes for numeric keypad.
  199.  * Alternately (for example) you could set the types to KCF_STRING
  200.  * and set the mappings to point to null terminated strings.
  201.  */
  202.  
  203. keymappings = appkeymap->km_LoKeyMap;
  204. keymaptypes = appkeymap->km_LoKeyMapTypes;
  205.  
  206. for(i=0; rawkeynum = nullkeys[i]; i++)
  207.     {
  208.     /* Because we allocated each of our Lo and Hi array pairs as
  209.      * sequential memory, we can use the RAWKEY values directly
  210.      * to index into our sequential Lo/Hi array
  211.      */
  212.     keymaptypes[rawkeynum] = KCF_NOP;
  213.     }
  214. }
  215.  
  216.  
  217. static void CopyKeyMap(struct KeyMap *s, struct KeyMap *d)
  218. {
  219. UBYTE *bb;
  220. ULONG *ll;
  221. int i;
  222.  
  223. /* Copy keymap s (source) to keymap d (dest) */
  224. for (i=0,ll=s->km_LoKeyMap;i<MAP_SIZE; i++) d->km_LoKeyMap[i] = *ll++;
  225. for (i=0,ll=s->km_HiKeyMap;i<MAP_SIZE; i++) d->km_HiKeyMap[i] = *ll++;
  226.  
  227. for (i=0, bb=s->km_LoKeyMapTypes; i<TYPE_SIZE; i++) d->km_LoKeyMapTypes[i] = *bb++;
  228. for (i=0, bb=s->km_HiKeyMapTypes; i<TYPE_SIZE; i++) d->km_HiKeyMapTypes[i] = *bb++;
  229.  
  230. for (i=0, bb=s->km_LoCapsable; i<CAPS_SIZE; i++) d->km_LoCapsable[i] = *bb++;
  231. for (i=0, bb=s->km_HiCapsable; i<CAPS_SIZE; i++) d->km_HiCapsable[i] = *bb++;
  232.  
  233. for (i=0, bb=s->km_LoRepeatable; i<REPS_SIZE; i++) d->km_LoRepeatable[i] = *bb++;
  234. for (i=0, bb=s->km_HiRepeatable; i<REPS_SIZE; i++) d->km_HiRepeatable[i] = *bb++;
  235. }
  236.